/*
* Copyright 2017 Yonyou Auto Information Technology（Shanghai） Co., Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project Name : marketing-common
*
* @File name : PostVedio.java
*
* @Author : Administrator
*
* @Date : 2017年12月25日
*
----------------------------------------------------------------------------------
*     Date       Who       Version     Comments
* 1. 2017年12月25日    Administrator    1.0
*
*
*
*
----------------------------------------------------------------------------------
*/

package com.yonyou.gmmc.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.client.ClientProtocolException;

/**
 * @author Administrator TODO description
 * @date 2017年12月25日
 */

public class PostVedio {

    public static String postFile(String url, String filePath,

                                  String title, String introduction) {

        File file = new File(filePath);

        if (!file.exists())

            return null;

        String result = null;

        try {

            URL url1 = new URL(url);

            HttpURLConnection conn = (HttpURLConnection) url1.openConnection();

            conn.setConnectTimeout(5000);

            conn.setReadTimeout(30000);

            conn.setDoOutput(true);

            conn.setDoInput(true);

            conn.setUseCaches(false);

            conn.setRequestMethod("POST");

            conn.setRequestProperty("Connection", "Keep-Alive");

            conn.setRequestProperty("Cache-Control", "no-cache");

            String boundary = "-----------------------------" + System.currentTimeMillis();

            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

            OutputStream output = conn.getOutputStream();

            output.write(("--" + boundary + "\r\n").getBytes());

            output.write(String.format("Content-Disposition: form-data; name=\"media\"; filename=\"%s\"\r\n",
                                       file.getName()).getBytes());

            output.write("Content-Type: video/mp4 \r\n\r\n".getBytes());

            byte[] data = new byte[1024];

            int len = 0;

            FileInputStream input = new FileInputStream(file);

            while ((len = input.read(data)) > -1) {

                output.write(data, 0, len);

            }

            output.write(("--" + boundary + "\r\n").getBytes());

            output.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n".getBytes());

            output.write(String.format("{\"title\":\"%s\", \"introduction\":\"%s\"}", title, introduction).getBytes());

            output.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes());

            output.flush();

            output.close();

            input.close();

            InputStream resp = conn.getInputStream();

            StringBuffer sb = new StringBuffer();

            while ((len = resp.read(data)) > -1)

                sb.append(new String(data, 0, len, "utf-8"));

            resp.close();

            result = sb.toString();

            System.out.println(result);

        } catch (ClientProtocolException e) {

            System.out.println("postFile，不支持http协议"+e);

        } catch (IOException e) {

            System.out.println("postFile数据传输失败"+e);

        }

        System.out.println("{}: result={}"+url+result);

        return result;

    }

    public static void main(String[] args) {
        
        String token = "5_GW8oxhBv_a0LwcsOtEvs0tHYVg3olROQpDTFuDWbGrGVLCuq7-hQE95HLDg8mA7VhZdjgNDeZJ8p7VShFHQziY2L19VukvuBF7LVefiCE8JtPak6249wx6nQgMxnfSrRyfR1Un52ZsH6elomJSFeAEAFOB";

        String url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="

                     + token + "&type=video";

        String result = postFile(url, "C:\\Users\\Administrator\\Desktop\\f40486045e447abbe5c18bd42d9918e3.mp4", "shiping", "miaoshu");

        System.out.println("Result为：");
        System.out.println(result);
    }
}
